package net.facelib.live;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * 产品自定义活体状态状态信息
 * @author guyadong
 *
 */
public class NativeStatus implements NativeLiveStatus{
	private static final ThreadLocal<NativeStatus> tlsStatus = new ThreadLocal<>();
	private final NativeLiveStatus delegate;
	private final int code;
	private final String message;
	public NativeStatus(int code,String message) {
		this.delegate = null;
		this.code = code;
		this.message = message;
	}
	public NativeStatus(NativeLiveStatus nativeLiveStatus) {
		this.delegate = checkNotNull(nativeLiveStatus,"nativeLiveStatus is null");
		this.code = delegate.getCode();
		this.message = delegate.getMessage();
	}
	
	public static void setNativeStatus(NativeStatus nativeStatus){
		tlsStatus.set(nativeStatus);
	}
	
	public static NativeStatus getNativeStatus(){
		return tlsStatus.get();
	}
	@Override
	public int getCode() {
		return code;
	}

	@Override
	public String getMessage() {
		return message;
	}

	@Override
	public LiveStatus asLiveStatus() {
		if( null == delegate){
			throw new UnsupportedOperationException();
		}
		return delegate.asLiveStatus();
	}
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("NativeStatus [code=");
		builder.append(code);
		builder.append(", message=");
		builder.append(message);
		builder.append("]");
		return builder.toString();
	}
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + code;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof NativeStatus))
			return false;
		NativeStatus other = (NativeStatus) obj;
		if (code != other.code)
			return false;
		return true;
	}
}